package com.baibei.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;

/**
 * 文本输入框
 * Created by ChenRui on 2017/8/7 0007 17:16.
 */
public class BaibeiEditText extends android.support.v7.widget.AppCompatEditText {

    private int mMaxValue;
    private int mMinValue = 0;
    private int mIncreaseValue = 1; // 自增


    public BaibeiEditText(Context context) {
        super(context);
    }

    public BaibeiEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public BaibeiEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BaibeiEditText);
        mMaxValue = a.getInt(a.getIndex(R.styleable.BaibeiEditText_maxValue), mMaxValue);
        a.recycle();
    }

    /**
     * 递增
     */
    public void increase() {
        int value = parseInt() + mIncreaseValue;
        if (mMaxValue > 0 && value > mMaxValue)
            value = mMaxValue;
        setText(String.valueOf(value));
        setSelection(length());
        requestFocusFromTouch();
    }

    /**
     * 设置自增比例
     */
    public void setIncreaseValue(int increaseValue) {
        mIncreaseValue = increaseValue;
    }

    /**
     * 递减
     */
    public void decrease() {
        int value = parseInt() - mIncreaseValue;
        if (value < mMinValue)
            value = mMinValue;
        setText(String.valueOf(value));
        setSelection(length());
        requestFocusFromTouch();
    }

    protected int parseInt() {
        if (TextUtils.isEmpty(getText())) return 0;
        try {
            return Integer.parseInt(getText().toString());
        } catch (Exception ignored) {
        }
        return 0;
    }

    public int getValue() {
        return parseInt();
    }

    //
    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        if (!checkValue()) return;
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
    }
//
//    @Override
//    public void setText(CharSequence text, BufferType type) {
//        if (!checkValue()) return;
//        super.setText(text, type);
//    }

    private boolean checkValue() {
        int value = parseInt();
        if (mMaxValue > 0 && value > mMaxValue) {
            setText(String.valueOf(mMaxValue));
            setSelection(length());
            return false;
        }
        if (value < mMinValue) {
            setText(String.valueOf(mMinValue));
            setSelection(length());
            return false;
        }
        return true;
    }

    public void setMaxValue(int maxValue) {
        mMaxValue = maxValue;
    }

    public void setMinValue(int minValue) {
        mMinValue = minValue;
    }
}
